package cn.ztuo.bitrade.entity;

import lombok.Data;

import javax.persistence.*;
import java.io.Serializable;
import java.math.BigDecimal;
import java.util.List;

/**
 * @Description
 * @Author Hunter
 * @Date 2020-03-05
 */

@Data
@Entity
@Table(name = "contract_order")
public class ContractOrder implements Serializable {

    private static final long serialVersionUID = 3335188963189793331L;

    /**
     * 订单ID
     */
    @Id
    @Column(name = "order_id")
    private String orderId;

    /**
     * 交易对
     */
    @Column(name = "symbol")
    private String symbol;

    /**
     * 买入卖出数量(同币币交易)   买入卖出交易币数量(amount)  =   交易的合约数量 * 最小委托交易币数量(交易对明细记录)
     */
    @Column(name = "amount")
    private BigDecimal amount;


    /**
     * 交易区
     */
    @Column(name = "base_symbol")
    private String baseSymbol;

    /**
     * 取消时间
     */
    @Column(name = "canceled_time")
    private Long canceledTime;

    /**
     * 交易币种
     */
    @Column(name = "coin_symbol")
    private String coinSymbol;

    /**
     * 起始保证金
     */
    @Column(name = "start")
    private BigDecimal start;

    /**
     * 起始保证金率
     */
    @Column(name = "start_rate")
    private String startRate;

    /**
     * 维持保证金
     */
    @Column(name = "maintain")
    private BigDecimal maintain;

    /**
     * 维持保证金率
     */
    @Column(name = "maintain_rate")
    private String maintainRate;

    /**
     * 起始仓位价值
     */
    @Column(name = "position_value")
    private BigDecimal positionValue;

    /**
     * 交易的合约数量  =  买入卖出交易币数量(amount) / 最小委托交易币数量(交易对明细记录)
     */
    @Column(name = "nowp")
    private BigDecimal nowp ;

    /**
     * 完成交易时间
     */
    @Column(name = "completed_time")
    private Long completedTime;

    /**
     * 订单方向
     */
    @Column(name = "direction")
    private ContractOrderDirection direction;

    /**
     * 用户
     */
    @Column(name = "member_id")
    private Long memberId;

    /**
     * 价格
     */
    @Column(name = "price")
    private BigDecimal price;

    /**
     * 1完成0未完成
     */
    @Column(name = "status")
    private ContractOrderStatus status;

    /**
     * 挂单时间
     */
    @Column(name = "list_time")
    private Long listTime;

    /**
     * 设置时间
     */
    @Column(name = "set_time")
    private Long setTime;

    /**
     * 成交量
     */
    @Column(name = "traded_amount")
    private BigDecimal tradedAmount ;

    /**
     * 成交额（市价）
     */
    @Column(columnDefinition = "decimal(18,8) DEFAULT 0 ")
    private BigDecimal turnover;

    /**
     * '1限价2市价3计划',
     */
    @Column(name = "type")
    private ContractOrderType type;

    /**
     * 触发价
     */
    @Column(name = "trigger_price")
    private BigDecimal triggerPrice ;

    /**
     * 倍数
     */
    @Column(name = "multiple")
    private BigDecimal multiple;

    /**
     * 仓位类型 0:开多 1:开空
     */
    @Column(name = "position_type")
    private ContractOrderPositionType positionType;

    /**
     * 平仓标记 0: 开仓 1:平仓
     */
    @Column(name = "position_flag")
    private ContractEnumType.ContractOrderPositionFlag positionFlag;

    /**
     * 平仓操作类型  0:主动平仓 1:强平
     */
    @Column(name = "position_opration_type")
    private ContractEnumType.ContractOrderPositionOprationType positionOprationType = null;
    /**
     * 止盈止损类型 1 : 止盈  2: 止损
     */
    @Column(name = "stop_type")
    private Integer stopType =0;

    /**
     * 是否来自用户交易
     */
    @Column(name = "order_resource")
    private ContractOrderResource orderResource;
    /**
     * 盈亏额
     */
    @Column(name = "profit_loss_amount")
    private BigDecimal profitLossAmount;
    /**
     * 盈亏率
     */
    @Column(name = "profit_loss_rate")
    private BigDecimal profitLossRate;
    /**
     * 手续费
     */
    @Column(name = "fee")
    private BigDecimal fee ;
    /**
     * 手续费
     */
    @Column(name = "trade_order_id")
    private String tradeOrderId ;
    /**
     * 强平仓位记录表id
     */
    @Column(name = "position_force_id")
    private Long positionForceId;

    @Transient
    private String username;


    @Transient
    private List<ContractOrderDetail> detail;
    @Transient
    private String amountStr;
    @Transient
    private String priceStr;
    //可用数量
    @Transient
    private int subNowp = 0;
//    public BigDecimal getSubNowp(){
//        ContractCoin coin = ContractUtils.getBaseSymbol(this.symbol);
//        return this.nowp.subtract(tradedAmount.divide(coin.getMinVolume())) ;
//    }
    //已成交数量
    @Transient
    private int tradeNowp = 0;
    //实现盈亏
    @Transient
    private BigDecimal yingKui = BigDecimal.ZERO;
//    public BigDecimal getTradeNowp(){
//        ContractCoin coin = ContractUtils.getBaseSymbol(this.symbol);
//        return tradedAmount.divide(coin.getMinVolume()) ;
//    }

    public boolean isCompleted() {
        if (status != ContractOrderStatus.TRADING) return true;
        else {
            return amount.compareTo(tradedAmount) <= 0;
        }
    }

}
